<?php
namespace App\Admin\Controllers\Traits\WxLckDrawTraits;

use App\Admin\Renderable\ShopGoodsProductRender;
use App\Models\Shop\WxShopGoodsProduct;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formLuckDrawTrait{
    public function index_config(Content $content)
    {
        return $content->title('抽奖转盘')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_config()->render()}</div>
HTML
            );
    }
    protected function form_config(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();

        $form->image('app_luck_draw_background', '页面背景')->help('<a href="https://img.mini.minisns.cn/hbx/video/luckdrawtopbg.png" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()
            ->default(Settings::get('app_luck_draw_background', 'https://img.mini.minisns.cn/hbx/video/luckdrawtopbg.png'));
        $form->html(function () {
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">小提示: 用户的每次抽奖可以在菜单[用户管理-抽奖]中查看</div>';
        }, '');
        $form->number('app_luck_draw_coin_pay', '每次抽奖花费')->help('不要填奇怪的数字，比如负数')->default(Settings::get('app_luck_draw_coin_pay', 20));
        $form->number('app_luck_draw_times', '免费抽奖次数')->help('不要填奇怪的数字，比如负数')->default(Settings::get('app_luck_draw_times', 3));
        $form->html(function () {
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">示例: <br>本次活动由XXXXXXX发起，与Apple.Inc无关。
<br>苹果公司不是本活动的赞助商且没有以任何形式参与活动。<br>本活动仅限17岁以上用户参加。<br>本活动最终解释权归XXXXXXX所有。</div>';
        }, '');
        $form->table('app_luck_draw_tips','抽奖说明', function (NestedForm $table) {
            $table->text('text', '说明')->default('本次活动由XXXXXXX发起，与Apple.Inc无关。');
        })->default(Settings::get('app_luck_draw_tips', ''));
        $form->html(function () {
            $res26 = __system__paycode__tip__(26);
            Tooltip::make('.mini_global_admin_open_not_support_tip')
                ->bottom()
                ->title($res26['tip']);
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">Tips: 1、[权重]决定了中奖概率，[权重]越大，奖励概率越大<br>2、类型和数量是用户实际获得的，你应该在奖项名字里提及，不会特意显示。<br>3、图片不适必须的，仅仅为后面的实物扩展准备<br>4、如需要谢谢惠顾等奖项，只需在奖项名字里填：谢谢参与，然后类型选择金币，数值选择0
<br>5、商品规格在奖励为商品时，才需要选择，若抽中了商品，相当于你购买一次该商品，该功能需要模块26，'.$res26['text'].'</div>';
        }, '');
        $form->table('app_luck_draw','转盘抽奖', function (NestedForm $table) {
            $table->text('id', 'id')->help('保证唯一性')->required()->default('l001');
            $table->image('img', '图片')->help('建议图片 100*100 px。 <a href="https://img.mini.minisns.cn/images/luck/coin.png" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload();
            $table->text('name', '奖项名称')->default('奖品一')->required();
            $table->number('weight', '权重')->default('99999')->required();
            $table->select('type', '奖励类型')->options(FieldUtils::getGiftTypes('luckdraw'))->default(0)->required();
            $table->number('num', '奖励数量')->default(1)->required();

            $table->multipleSelectTable('product', '商品规格')
                ->title('商品规格库')
                ->max(1)
                ->from(ShopGoodsProductRender::make())
                ->model(WxShopGoodsProduct::class, 'id', 'id');

        })->default(Settings::get('app_luck_draw', ''));

        $form->action('luckdraw/action/config');
        return $form;
    }

    public function config(Request $request){

        $luck_draw_id_arr = [];
        if(is_array($request->input('app_luck_draw'))){
            $app_luck_draw = $request->input('app_luck_draw');
            if($app_luck_draw){
                foreach ($app_luck_draw as $luck_draw){
                    if($luck_draw['_remove_'] == 1){
                        continue;
                    }
                    if(in_array($luck_draw['id'], $luck_draw_id_arr)){
                        return $this->response()->error('抽奖选项的ID重复了，请保持各项唯一');
                    }else{
                        $luck_draw_id_arr[] = $luck_draw['id'];
                    }
                }
            }
        }

        SiteUtils::forget_config_data();


        Settings::set('app_luck_draw_background', $request->input('app_luck_draw_background'), true);
        Settings::set('app_luck_draw_coin_pay', $request->input('app_luck_draw_coin_pay'), true);
        Settings::set('app_luck_draw_times', $request->input('app_luck_draw_times'), true);
        Settings::set('app_luck_draw_tips', AdminUtils::_table_data($request->input('app_luck_draw_tips')), true);
        Settings::set('app_luck_draw', AdminUtils::_table_data($request->input('app_luck_draw')), true);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
